// https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
        if (!n) return 0;
        
        int ans = 1, left = 0, right = 0;
        unordered_map<char, int> mp;
        while (right < n) {
            if (mp.count(s[right]) > 0) {
                ans = max(right - left, ans);

                for (int i = left; i < mp[s[right]]; i++) {
                    mp.erase(s[i]);
                }

                left = mp[s[right]] + 1;
                mp[s[right]] = right;
            } else {
                mp[s[right]] = right;
            }
            right++;
        }
        return max(ans, right - left);
    }
};